package demo.hdz.mycustomview.customview;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;

import demo.hdz.mycustomview.utils.Logger;

/**
 * @author hdz
 * @date 2018/11/29
 */
@SuppressLint("AppCompatCustomView")
public class CustomImageView extends ImageView {

    private static final String TAG = "CustomImageView";

    private int mWidth = 0;
    private int mHeight = 0;
    private int mImageAlpha = 0;

    public CustomImageView(Context context) {
        this(context, null);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mWidth = this.getWidth();
        mHeight = this.getHeight();
        Logger.d(TAG+",CustomImageView: mWidth=" + mWidth + ", mHeight=" + mHeight);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        ViewGroup.LayoutParams params = this.getLayoutParams();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                zoomSize(params);
                mImageAlpha = this.getImageAlpha();
                this.setImageAlpha(200);
                break;
            case MotionEvent.ACTION_UP:
                params.height=mHeight;
                params.width =mWidth;
                this.setLayoutParams(params);
                this.setImageAlpha(mImageAlpha);
                performClick();
                break;
            default:
                return super.onTouchEvent(event);
        }
        return true;
    }

    private void zoomSize(ViewGroup.LayoutParams params) {
        mHeight = params.height;
        mWidth = params.width;
        int tmpW = mWidth + 50;
        int tmpH = mHeight + 50;
        Logger.d(TAG+",CustomImageView: mWidth=" + mWidth + ", mHeight=" + mHeight);
        Logger.d(TAG+",CustomImageView: mWidth=" + tmpW + ", mHeight=" + tmpH);
        params.height = tmpH;
        params.width = tmpW;
        this.setLayoutParams(params);
    }

}
